#include "volumetoolwidget.h"
#include "ui_volumetoolwidget.h"
#include <QGraphicsDropShadowEffect>
#include <QDebug>
#include <QPainter>
VolumeToolWidget::VolumeToolWidget(QWidget *parent) :
    QWidget(parent),
    _noVolume(false),
    _curVolume(20),
    ui(new Ui::VolumeToolWidget)
{
    ui->setupUi(this);

    setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
    // 弹窗默认带阴影,要用NoDropShadowWindowHint去掉.
    // 设置阴影:
    setAttribute(Qt::WA_TranslucentBackground);
    QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect(this);
    effect->setColor(QColor(0, 0, 0, 50));
    effect->setOffset(0, 0);
    effect->setBlurRadius(20);
    setGraphicsEffect(effect);

    setVolume(_curVolume);

    connect(ui->slider, &Slider::sliderClicked, this, &VolumeToolWidget::setVolume);
    connect(ui->slider, &Slider::valueChanged, this, &VolumeToolWidget::setVolume);
}

VolumeToolWidget::~VolumeToolWidget()
{
    delete ui;
}

void VolumeToolWidget::setVolume(int volume)
{
    ui->volume->setText(QString::number(volume) + "%");
    ui->slider->setValue(volume);
    if(volume) _curVolume = volume; // 如果为0则认为静音,不设置_curVolume
    qDebug() << "Set player volume1:" << volume;
    emit volumeChanged(volume);
}

void VolumeToolWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::NoPen);
    painter.setBrush(Qt::white);
    QPolygon polygon;
    polygon << QPoint(28, 210) << QPoint(52, 210) << QPoint(40, 222);
    painter.drawPolygon(polygon);
    QWidget::paintEvent(event);
}

void VolumeToolWidget::on_pushButton_clicked()
{
    if (_noVolume){
        setVolume(_curVolume);
    } else {
        setVolume(0);
    }
    _noVolume = !_noVolume;
}
